package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.AnnouncementMapper;
import com.j283_5.wisdom_park.mapper.RoomMapper;
import com.j283_5.wisdom_park.mapper.TenementMapper;
import com.j283_5.wisdom_park.pojo.Announcement;
import com.j283_5.wisdom_park.pojo.Room;
import com.j283_5.wisdom_park.pojo.Tenement;
import com.j283_5.wisdom_park.service.AnnouncementService;
import com.j283_5.wisdom_park.vo.asset_management.AnnouncementVo;
import com.j283_5.wisdom_park.vo.asset_management.InsertAnnRoomVo;
import com.j283_5.wisdom_park.vo.contract_management.RoomVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class AnnouncementServiceImpl  implements AnnouncementService {
    @Resource
    private AnnouncementMapper mapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private TenementMapper tenementMapper;
    /**
     * 多条件查询资源信息
     * @param ann_name
     * @return
     */
    @Override
    public ResultData findAnnouncement(String ann_name, Page<Announcement> page) {
        QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
        if (ann_name!=null){
            announcementQueryWrapper.like("ann_name",ann_name);

        }
        announcementQueryWrapper.eq("ann_delete",0);
        Page<Announcement> announcementPage = mapper.selectPage(page, announcementQueryWrapper);
        return ResultData.ok().data(announcementPage) ;
    }

    /**
     * 根据资源id查询详情
     * @return
     */
    @Override
    public ResultData findAnnouncementById(Integer ann_Id) {
        List<AnnouncementVo> announcementById = mapper.findAnnouncementById(ann_Id);
        return  ResultData.ok().data(announcementById);
    }

    /**
     * 插入资源
     * @return
     */
    @Override
    public ResultData insertAnnouncement(AnnouncementVo announcementVo) {
        List<RoomVo> list = roomMapper.RoomTenVo(announcementVo.getRoomId());
        RoomVo roomVo = list.get(0);
        StringBuffer stringBuffer=new StringBuffer(roomVo.getTenSerialint()+"-"+roomVo.getRoomNo());
        Announcement announcement=new Announcement();
        announcement.setAnnName(announcementVo.getAnnName());
        announcement.setAnnMoney(announcementVo.getAnnMoney());
        announcement.setAnnArea(announcementVo.getAnnArea());
        announcement.setAnnType(announcementVo.getAnnType());
        announcement.setAnnNum(announcementVo.getAnnNum());
        announcement.setRoomId(announcementVo.getRoomId());
        announcement.setAnnWhere(announcementVo.getAnnWhere());
        announcement.setAnnResources(announcementVo.getAnnResources());
        announcement.setAnnHomephoto(announcementVo.getAnnHomephone());
        announcement.setAnnSerialint(stringBuffer.toString());
        announcement.setRoomNo(roomVo.getRoomNo());
        int insert = mapper.insert(announcement);
        if (insert>0){
            return ResultData.ok().message("插入成功");
        }else {
            return ResultData.ok().message("插入失败");
        }
    }

    /**
     * 资源删除
     * @param annId
     * @return
     */
    @Override
    public ResultData deleteAnnouncement(List<Integer> annId) {
        UpdateWrapper<Announcement> AnnouncementWrapper = new UpdateWrapper<>();
        AnnouncementWrapper.set("ann_delete",1);
        AnnouncementWrapper.in("ann_Id",annId);
        int i = mapper.update(null, AnnouncementWrapper);
        if(i > 0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.error().message("删除失败！");
    }
    /**
     * 进入资源新增界面
     * @return
     */
    @Override
    public ResultData AnnRoom() {
        List<InsertAnnRoomVo> insertAnnRoomVos = roomMapper.AnnRoom();
        return ResultData.ok().data(insertAnnRoomVos);
    }
}
